package com.yydream.common.exception;

import com.fasterxml.jackson.core.JsonParseException;
import com.yydream.common.constant.HttpStatus;
import com.yydream.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.List;

@Slf4j
@RestControllerAdvice
public class ExceptionControllerAdvice {
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public R handleVaildException (MethodArgumentNotValidException e) {
        HashMap<String, String> map = new HashMap<>();
        BindingResult bindingResult = e.getBindingResult();
        // 获取到所有字段的错误集合
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        fieldErrors.forEach(c -> {
            // 获取错误字段
            String field = c.getField();
            // 获取错误的描述
            String message = c.getDefaultMessage();
            map.put(field, message);
        });
        return R.error(HttpStatus.PARAM_ERROR.getCode(), HttpStatus.PARAM_ERROR.getMsg()).put( map);
    }
    @ExceptionHandler(value = BadCredentialsException.class)
    public R handleAuthenticationException (BadCredentialsException e) {
        String message = e.getMessage();
        return R.error(HttpStatus.PARAM_ERROR.getCode(), message);
    }
    @ExceptionHandler(value = InternalAuthenticationServiceException.class)
    public R handleInternalAuthenticationServiceException (InternalAuthenticationServiceException e) {
        String message = e.getMessage();
        return R.error(HttpStatus.SYSTEM_ERROR.getCode(), message);
    }
    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    public R handleMissingServletRequestParameterException (MissingServletRequestParameterException e) {
        String message = e.getMessage();
        return R.error(HttpStatus.PARAM_ERROR.getCode(), HttpStatus.PARAM_ERROR.getMsg()).put( message);
    }
    @ExceptionHandler(value = CustomException.class)
    public R handleCustomException (CustomException e) {
        return R.error(e.getCode(), e.getMsg());
    }

    /**
     * 参数转换异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public R handleHttpMessageNotReadableException (HttpMessageNotReadableException e) {
        return R.error(HttpStatus.PARAM_ERROR.getCode(), HttpStatus.PARAM_ERROR.getMsg()).put( "参数转换异常");
    }

    /**
     * 全局异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public R handlerException (Exception e) {
        String message = e.getMessage();
        log.error(message, e);
        return R.error(HttpStatus.SYSTEM_ERROR.getCode(), HttpStatus.SYSTEM_ERROR.getMsg());
    }
}
